import { reqgetbanner, reqgetcate, reqgetindexgoods, reqgetnew, reqgetgroupon, reqgetseckill, reqgetcountdown } from '../../http/api'

const initState = {
    cate: [],
    banner: [],
    goods: [],
    newcomers: [],
    n: 0,
    getgroup: [],
    seckill: [],
   
  
}
const TYPES = {
    //cate type
    HOME_CHANGE_CATE: 'HOME_CHANGE_CATE',
    //banner
    HOME_CHANGE_BANNER: "HOME_CHANGE_BANNER",
    //goods
    HOME_CHANGE_GOODS: "HOME_CHANGE_GOODS",
    //newcomers
    HOME_CHANGE_NEWCOMERS: 'HOME_CHANGE_NEWCOMERS',
    //n
    HOME_CHANGE_N: "HOME_CHANGE_N",
    //getgroup
    HOME_CHANGE_GETGROUP: 'HOME_CHANGE_GETGROUP',
    // seckill
    HOME_CHANGE_SECKILL: ' HOME_CHANGE_SECKILL',
    //time
    // HOME_TIME:'HOME_TIME'

}
//reducer
const reducer = (state = initState, action) => {
    switch (action.type) {
        case TYPES.HOME_CHANGE_CATE:
            return {
                ...state,
                cate: action.cate
            }
        //轮播
        case TYPES.HOME_CHANGE_BANNER:
            return {
                ...state,
                banner: action.banner
            }
        //商品
        case TYPES.HOME_CHANGE_GOODS:
            return {
                ...state,
                goods: action.goods
            }
        //新人专享
        case TYPES.HOME_CHANGE_NEWCOMERS:
            return {
                ...state,
                newcomers: action.newcomers
            }
        //n
        case TYPES.HOME_CHANGE_N:
            return {
                ...state,
                n: action.n
            }
        //万人团 getgroup
        case TYPES.HOME_CHANGE_GETGROUP:
            return {
                ...state,
                getgroup: action.getgroup
            }
        //秒杀 seckill
        case TYPES.HOME_CHANGE_SECKILL:
            return {
                ...state,
                seckill: action.seckill
            }
          
        default:
            return state;

    }
}
//action creator
export const actions = {
    changeCate:cate=>({type:TYPES.HOME_CHANGE_CATE,cate}),

    //请求cate
    reqCate:()=>(dispatch,getState)=>{
        let cates = getCate(getState())
        if(cates.length>0){
            return;
        }
        reqgetcate().then(res=>{
            //修改cate
            dispatch(actions.changeCate(res.data.list))

        })
    },
    //banner
    changeBanner: (banner) => ({ type: TYPES.HOME_CHANGE_BANNER, banner }),
    // 2.5 请求banner 
    reqBanner: () => (dispatch, getState) => {
        let banner = getBanner(getState())
        if (banner.length > 0) {
            return;
        }
        reqgetbanner().then(res => {
            //修改banner
            dispatch(actions.changeBanner(res.data.list))
        })
    },
    //goods action
    changeGoods: goods => ({ type: TYPES.HOME_CHANGE_GOODS, goods }),
    // 3.5 请求goods 
    reqGoods: () => (dispatch, getState) => {
        let goods = getGoods(getState())
        if (goods.length > 0) {
            return;
        }
        reqgetindexgoods().then(res => {
            dispatch(actions.changeGoods(res.data.list))
        })
    },
    //新人专享
    changeNewcomers: newcomers => ({ type: TYPES.HOME_CHANGE_NEWCOMERS, newcomers }),
    // 请求new 
    reqNewcomers: () => (dispatch, getState) => {
        let newcomers  = getNewcomers(getState())
        if (newcomers.length > 0) {
            return;
        }
        reqgetnew().then(res => {
            dispatch(actions.changeNewcomers(res.data.list))
        })
    },
    //修改n
    changeN: n => ({ type: TYPES.HOME_CHANGE_N, n }),
    //万人团
    changeGetgroup: getgroup => ({ type: TYPES.HOME_CHANGE_GETGROUP, getgroup }),
    // 请求new 
    reqGetgroup: () => (dispatch, getState) => {
        let getgroup  = getGetgroup(getState())
        if (getgroup.length > 0) {
            return;
        }
        reqgetgroupon().then(res => {
            dispatch(actions.changeGetgroup(res.data.list))
        })
    },
    //秒杀活动
    changeSeckill: seckill => ({ type: TYPES.HOME_CHANGE_SECKILL, seckill }),
    // 请求new 
    reqSeckill: () => (dispatch, getState) => {
        let seckill  = getSeckill(getState())
        if (seckill.length > 0) {
            return;
        }
        reqgetseckill().then(res => {
            dispatch(actions.changeSeckill(res.data.list))
        })
    },
    

}
export const getCate = state => state.home.cate
// banner selector 
export const getBanner = state => state.home.banner
//  goods selector 
export const getGoods = state => state.home.goods
// getNewcomers
export const getNewcomers = state => state.home.newcomers
// n selector 
export const getN = state => state.home.n
// getgroup
export const getGetgroup = state => state.home.getgroup
//seckill
export const getSeckill = state => state.home.seckill
// //goodlist
// export const getGoodList = state=> state.home.goodsList


//导出reducer
export default reducer;